package org.example.erpapicommon.Util;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

import java.util.Objects;


@JsonInclude(Include.NON_NULL)
public class ApiResult<T> {

    private Integer code;

    private boolean success;

    private String message;
    @JsonInclude(Include.NON_NULL)

    private T data;

    public ApiResult() {
    }

    public static <T> ApiResult<T> success(T data) {
        return success(data, "请求成功");
    }

    public static <T> ApiResult<T> success(T data, String message) {
        ApiResult<T> result = new ApiResult();
        result.setSuccess(true).setCode(ResponseType.OK.code()).setData(data).setMessage(message);
        return result;
    }

    public static <T> ApiResult<T> fail(ResponseType responseType, String message) {
        ApiResult<T> result = new ApiResult();
        result.setMessage(message).setCode(responseType.code());
        result.setSuccess(false);
        return result;
    }

    public static <T> ApiResult<T> fail(ResponseType responseType) {
        return fail(responseType, responseType.message());
    }

    public static <T> ApiResult<T> create(ResponseType responseType, T data, String message) {
        ApiResult<T> result = new ApiResult();
        result.setMessage(message).setCode(responseType.code()).setData(data);
        result.setSuccess(responseType == ResponseType.OK);
        return result;
    }

    public static <T> ApiResult<T> create(ResponseType responseType, T data) {
        return create(responseType, data, responseType.message());
    }

    public Integer getCode() {
        return this.code;
    }

    public ApiResult<T> setCode(Integer code) {
        this.code = code;
        return this;
    }

    public boolean isSuccess() {
        return this.success;
    }

    public ApiResult<T> setSuccess(boolean success) {
        this.success = success;
        return this;
    }

    public String getMessage() {
        return this.message;
    }

    public ApiResult<T> setMessage(String message) {
        this.message = message;
        return this;
    }

    public T getData() {
        return this.data;
    }

    public ApiResult<T> setData(T data) {
        this.data = data;
        return this;
    }

    public boolean equals(Object o) {
        if (this == o) {
            return true;
        } else if (o != null && this.getClass() == o.getClass()) {
            ApiResult<?> apiResult = (ApiResult)o;
            return this.code.equals(apiResult.code) && this.message.equals(apiResult.message);
        } else {
            return false;
        }
    }

    public int hashCode() {
        return Objects.hash(new Object[]{this.code, this.message});
    }

    public String toString() {
        return "ApiResult{code=" + this.code + ", success=" + this.success + ", message='" + this.message + "'}";
    }
}
